const express = require('express');
const router = express.Router();

const bodyParser = require('body-parser');

const data = require('./data');

//10086  到下一个10086间的是我修改的内容
const mysql = require('mysql')
const accountSQL = require('../db/accountSQL');
const appealSQL = require('../db/appealSQL');
const dbConfig = require('../db/dbConfig');
const managerSQL = require('../db/managerSQL');
const messageSQL = require('../db/messageSQL');
const replySQL = require('../db/replySQL');
const sectionSQL = require('../db/sectionSQL');
const topicSQL = require('../db/topicSQL');
const userSQL = require('../db/userSQL');


// 使用DBConfig.js的配置信息创建一个MySql链接池
var pool = mysql.createPool( dbConfig.mysql )
// 响应一个JSON数据
const responseJSON = function (res, ret) {
  if (typeof ret === 'undefined') {
    res.json({
      code: '-200',
      msg: '操作失败'
    })
  } else {
    res.json(ret)
  }
}
//10086

let UPDic = data.UPDic;

router.post('/register', bodyParser.json(), (req, res) => {
  pool.getConnection(function (err,connection) {
// 检查用户名与密码是否为空
    if (!req.body["username"]
      || typeof req.body["username"] != "string"
      || !req.body["username"].length) {
      res.json({
        err: 1,
        message: '用户名为空'
      })
    } else if (!req.body["password"]
      || typeof req.body["password"] != "string"
      || !req.body["password"].length) {
      res.json({
        err: 1,
        message: '密码为空'
      })
    }else {
      let username = req.body["username"];
      let password = req.body["password"];
      // 下方改数据库操作代码
      if (UPDic[username]) {
        res.json({
          err: 1,
          message: '用户名已存在'
        })
      } else {
        UPDic[username] = password;
        res.json({
          err: 200,
          message: '注册成功',
          username,
          password
        })
      }
    }
  })
})

module.exports = router;
